﻿// MainDlg.cpp: 实现文件
//

#include "pch.h"
#include "wowlua03.h"
#include "mgr.h"
#include "lua.h"


// MainDlg 对话框

IMPLEMENT_DYNAMIC(MainDlg, CDialogEx)

MainDlg::MainDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MAIN, pParent)
{

}

MainDlg::~MainDlg()
{
}

void MainDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TAB1, tab_);
}


BEGIN_MESSAGE_MAP(MainDlg, CDialogEx)
	ON_WM_CLOSE()
END_MESSAGE_MAP()


// MainDlg 消息处理程序


BOOL MainDlg::OnInitDialog() {
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	tab_.AddPage("Page1", &page1_, IDD_PAGE1);
	tab_.AddPage("Page2", &page2_, IDD_PAGE2);
	tab_.Show();

	HWND hwnd_game = get_main_wnd("GxWindowClass");
	uint32 pid = GetCurrentProcessId();
	uint64 module_base = get_module_base(pid).value_or(0);
	dbgprint("codergege [%s][%03d] 窗口句柄[0x%X]", __FUNCTION__, __LINE__, hwnd_game);
	dbgprint("codergege [%s][%03d] 游戏基址[0x%llX]", __FUNCTION__, __LINE__, module_base);

	Mgr::get()->init(pid, hwnd_game, module_base);
	Mgr::get()->game()->call()->attach_main_thread();

	dbgprint("codergege 注册LUA接口开始 [%s][%d]", __FUNCTION__, __LINE__);
	lua_reg();
	bypass_lua_invoke_checking();
	dbgprint("codergege 注册LUA接口结束 [%s][%d]", __FUNCTION__, __LINE__);


	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void MainDlg::OnClose() {
	Mgr::get()->game()->call()->detach_main_thread();
	CDialogEx::OnClose();
}
